package me.zhengjie.org.departmentbase.service.impl;

import me.zhengjie.org.departmentbase.domain.DepartmentBase;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.org.departmentbase.repository.DepartmentBaseRepository;
import me.zhengjie.org.departmentbase.service.DepartmentBaseService;
import me.zhengjie.org.departmentbase.service.dto.DepartmentBaseDto;
import me.zhengjie.org.departmentbase.service.dto.DepartmentBaseQueryCriteria;
import me.zhengjie.org.departmentbase.service.mapper.DepartmentBaseMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @author ZGZ
* @date 2020-04-15
*/
@Service
//@CacheConfig(cacheNames = "departmentBase")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DepartmentBaseServiceImpl implements DepartmentBaseService {

    private final DepartmentBaseRepository departmentBaseRepository;

    private final DepartmentBaseMapper departmentBaseMapper;

    public DepartmentBaseServiceImpl(DepartmentBaseRepository departmentBaseRepository, DepartmentBaseMapper departmentBaseMapper) {
        this.departmentBaseRepository = departmentBaseRepository;
        this.departmentBaseMapper = departmentBaseMapper;
    }

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(DepartmentBaseQueryCriteria criteria, Pageable pageable){
        Page<DepartmentBase> page = departmentBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(departmentBaseMapper::toDto));
    }

    @Override
    //@Cacheable
    public List<DepartmentBaseDto> queryAll(DepartmentBaseQueryCriteria criteria){
        return departmentBaseMapper.toDto(departmentBaseRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public DepartmentBaseDto findById(Integer id) {
        DepartmentBase departmentBase = departmentBaseRepository.findById(id).orElseGet(DepartmentBase::new);
        ValidationUtil.isNull(departmentBase.getId(),"DepartmentBase","id",id);
        return departmentBaseMapper.toDto(departmentBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public DepartmentBaseDto create(DepartmentBase resources) {
        return departmentBaseMapper.toDto(departmentBaseRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(DepartmentBase resources) {
        DepartmentBase departmentBase = departmentBaseRepository.findById(resources.getId()).orElseGet(DepartmentBase::new);
        ValidationUtil.isNull( departmentBase.getId(),"DepartmentBase","id",resources.getId());
        departmentBase.copy(resources);
        departmentBaseRepository.save(departmentBase);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            departmentBaseRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<DepartmentBaseDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (DepartmentBaseDto departmentBase : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("部门名称", departmentBase.getDepartmentName());
            map.put("创建时间", departmentBase.getStartTime());
            map.put("撤销时间", departmentBase.getEndTime());
            map.put("部门描述", departmentBase.getDescribeInfo());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}